package com.nidu.demo.i18n;

import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.i18n.api.SysI18nService;
import com.nidu.demo.i18n.service.I18nLocaleResolver;
import com.nidu.demo.i18n.dto.LanguageCO;
import com.nidu.demo.i18n.util.I18nUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Service
@RequiredArgsConstructor
public class SysI18nServiceImpl implements SysI18nService {

    @Qualifier("localeResolver")
    private final I18nLocaleResolver localeResolver;

    @Override
    public Response switchLanguage(String lang) {
        // 解析语言代码
        Locale locale = localeResolver.parseLocale(lang);
        if(locale == null){
            throw new BizException(ErrorCodeConstants.I18N_CODE_NOT_FOUND.getCode(), ErrorCodeConstants.I18N_CODE_NOT_FOUND.getMessage());
        }

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            HttpServletResponse response = attributes.getResponse();
            localeResolver.setLocale(request, response, locale);
        }
        return Response.buildSuccess();
    }

    @Override
    public SingleResponse<LanguageCO> getCurrentLanguage() {
        Locale currentLocale = localeResolver.getLocal();
        LanguageCO languageCO = LanguageCO.builder()
                .language(currentLocale.getLanguage())
                .country(currentLocale.getCountry())
                .displayName(currentLocale.getDisplayName())
                .locale(currentLocale.toString())
                .build();
        return SingleResponse.of(languageCO);
    }

    @Override
    public MultiResponse<LanguageCO> getSupportLanguages() {
        Map<String, Locale> supportedLanguages = localeResolver.getSupportedLanguages();
        List<LanguageCO> result = new ArrayList<>();
        for(Map.Entry<String, Locale> localeEntry : supportedLanguages.entrySet()){
            Locale locale = localeEntry.getValue();
            LanguageCO languageCO = LanguageCO.builder()
                    .language(locale.getLanguage())
                    .country(locale.getCountry())
                    .displayName(locale.getDisplayName())
                    .locale(locale.toString())
                    .build();
            result.add(languageCO);
        }
        return MultiResponse.of(result);
    }

    @Override
    public SingleResponse<String> getI18nMessage(String code) {
        String message = I18nUtil.getMessage(code);
        if(StrUtil.isEmpty(message) || message.equals(code)){
            throw new BizException(ErrorCodeConstants.I18N_CODE_NOT_FOUND.getCode(), ErrorCodeConstants.I18N_CODE_NOT_FOUND.getMessage());
        }
        return SingleResponse.of(message);
    }

}
